package com.lzb.newloan;


import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
import com.lzb.newloan.utils.AlertDialog;
import com.lzb.newloan.utils.BaseActivity;
import com.lzb.newloan.utils.Config;
import com.lzb.newloan.utils.Formatdou;
import com.lzb.newloan.utils.HttpUtils;
import com.lzb.newloan.view.MyProgressDialog;

import org.json.JSONException;
import org.json.JSONObject;

public class CameraActivity extends BaseActivity {
	private String userId;
	private OSSClient oss;
	private OSSAsyncTask task;
	private String realname;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_camera);

		SharedPreferences sp = getSharedPreferences("config", 0x0000);
		userId = sp.getString("userid", "");

		TextView title_txt_center = (TextView) findViewById(R.id.title_txt_center);
		title_txt_center.setText("上传视频");
		RelativeLayout backpress = (RelativeLayout) findViewById(R.id.backpress);
		backpress.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (task!=null) {
		    		task.cancel(); // 可以取消任务
				}
				finish();
			}
		});
		dialog = new MyProgressDialog(CameraActivity.this, "");
		pf_money = (TextView) findViewById(R.id.pf_money);
		sj_money = (TextView) findViewById(R.id.sj_money);
		jk_date = (TextView) findViewById(R.id.jk_date);
		jk_referne = (TextView) findViewById(R.id.jk_referne);
		low_referne = (TextView) findViewById(R.id.low_referne);
		fx_money = (TextView) findViewById(R.id.fx_money);
		Button confirm = (Button) findViewById(R.id.confirm);
		check = (CheckBox) findViewById(R.id.check);
		confirm.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (!check.isChecked()) {
					setDialog("请勾上借款说明");
					return;
				}
				new AlertDialog(CameraActivity.this).builder().setMsg("拍摄视频时请说出下列话："
				+Html.fromHtml("<font color=\"#346EFD\">"+"我叫"+ realname+"通过钱宝包申请借款"
						+"￥"+pf_money.getText().toString()+"</font>"))
				.setNegativeButton("确定", new OnClickListener() {
					@Override
					public void onClick(View v) {
//						finish();
						Intent intent = new Intent(CameraActivity.this,Camera2Activity.class);
						intent.putExtra("name",realname);
						intent.putExtra("money",pf_money.getText().toString());
						startActivity(intent);
						finish();
					}
				}).show();
			}
		});
		
		HttpUtils.doPostAsyn(Config.BACKMONEYINIT_CORD+"&jkid="+getIntent().getStringExtra("jkid"), "",mHandler,
				Config.CODE_BACKMONEYINIT);
		
		
	}
	
	/** 
	 * 捕捉back 
	 */  
	@Override  
	public boolean onKeyDown(int keyCode, KeyEvent event) {  
	    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {  
	    	if (task!=null) {
	    		task.cancel(); // 可以取消任务
			}
			finish();
	        return true;  
	        
	    }  
	      
	    return super.onKeyDown(keyCode, event);  
	}  
	
	
	private Handler mHandler = new Handler() {
	

		public void handleMessage(android.os.Message msg) {
			
			switch (msg.what) {
			case Config.CODE_URL_ERROR:
				Toast.makeText(CameraActivity.this, "url错误", Toast.LENGTH_SHORT)
						.show();
				break;
			case Config.CODE_NET_ERROR:
				Toast.makeText(CameraActivity.this, "网络错误", Toast.LENGTH_SHORT)
						.show();
				break;
			case 11:
				Toast.makeText(CameraActivity.this, "上传失败", Toast.LENGTH_SHORT)
				.show();
//				finish();
				dialog.dismiss();
				break;
			case Config.CODE_BACKMONEYINIT:
				String result = msg.obj.toString();
				try {
					JSONObject jsonObject = new JSONObject(result);
//					java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
					double all= jsonObject.optDouble("actual_amount"); //审批金额
					double real= jsonObject.optDouble("received_amount"); //实际到账金额
					pf_money.setText(Formatdou.formatdou(all)+"元");
					sj_money.setText(Formatdou.formatdou(real)+"元");
					int jk_dates = jsonObject.optInt("loan_term");//借款期限
					double annualrate = jsonObject.optDouble("annual_rate");
					

					double low = 0.0,fw=0.0,allfee=0.00;
					if (jk_dates==1) {
						jk_date.setText("15天");
						low = (all*0.03*0.5);
						fw = (all*0.27*0.5);
//						allfee = all*annualrate/100*0.5;
					}else {
						jk_date.setText("30天");
						low = (all*0.03);
						fw = (all*0.27);
//						allfee = all*annualrate/100;
					}
					jk_referne.setText(Formatdou.formatdou(annualrate));  //总费用
					low_referne.setText(Formatdou.formatdou(low));
					fx_money.setText(Formatdou.formatdou(fw));
					realname = jsonObject.optString("realname");

				} catch (JSONException e) {
					e.printStackTrace();
				}
				break;

			default:
				break;
			}
		};
	};
	private ProgressBar pb;
	private TextView tv;
	private CheckBox check;
	private TextView pf_money;
	private TextView sj_money;
	private TextView jk_date;
	private TextView jk_referne;
	private TextView low_referne;
	private TextView fx_money;
	private MyProgressDialog dialog;
	private void setDialog(String Message) {
		new AlertDialog(CameraActivity.this).builder().setMsg(Message)
				.setNegativeButton("确定", new OnClickListener() {
					@Override
					public void onClick(View v) {
//						finish();
					}
				}).show();
	}


	@Override
	public void processMessage(Message message) {
		// TODO Auto-generated method stub
		
	}

}